var IMconn = null ;
//_RT = null;
$(function() {
	
	mui.init();
 	var viewApi = mui('#app').view({
		defaultPage: '#tpl_home'
	});
	var view = viewApi.view;
	
	(function(M) {
		//处理view的后退与webview后退
		var oldBack = M.back;
		M.back = function() {
			//如果有弹出页面则先关闭弹出页面
			if($('.popPageShow').length>0){ 
				$('.popPageShow').removeClass('popPageShow');
			}else if (viewApi.canBack()) { //如果view可以后退，则执行view的后退
				viewApi.back();
			}  else { //执行webview后退
				oldBack();
			}
		};
		//注册页面显示的回调函数
		var viewCallBacks = ['pageBeforeShow','pageShow','pageBeforeBack','pageBack'];
		$.each(viewCallBacks, function(i,callback) {
			view.addEventListener(callback, function(e) {
				var fn = null;
				eval( ' fn = ' + e.detail.page.id+'.'+callback);
				if(typeof(fn) == 'function'){ fn();}
				console.log(e.detail.page.id + ' '+callback);
			});
		});
		pageFun.initPage();
	})(mui);
//	var router = new Router({
//		container: '#container',
//		enterTimeout: 10,
//		leaveTimeout: 10
//	});
//	_RT = router;

	//地址管理
	var tpl_Address = {
//		url: '/Address',
//		className: 'Address',
//		render: function() {
//			return $('#tpl_Address').html();
//		},
		bind: function() {
			bindFunction();
			pageFun.Address();
		}
	};
	//QQ绑定
	var QQ = {
		url: '/QQ',
		className: 'QQ',
		render: function() {
			return $('#tpl_QQ').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.user();  
		}
	};
	//微信
	var WeChat = {
		url: '/WeChat',
		className: 'WeChat',
		render: function() {
			return $('#tpl_WeChat').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.user();  
		}
	};
	//添加地址
	var addressadd = {
		url: '/addressadd',
		className: 'addressadd',
		render: function() {
			return $('#tpl_addressadd').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.addressadd();  
		}
	};
	//编辑地址
	var EditAddress = {
		url: '/EditAddress',
		className: 'EditAddress',
		render: function() {
			return $('#tpl_EditAddress').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.EditAddress();  
		}
	};
	//三级联动
	var LD = {
		url: '/LD',
		className: 'LD',
		render: function() {
			return $('#tpl_LD').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.LD();
		}
	};
	//报修
	var Repair = {
		url: '/Repair',
		className: 'Repair',
		render: function() {
			return $('#tpl_Repair').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Repair();  
		}
	};
	
	//店铺详情
	var shop = {
		url: '/shop/*',
		className: 'shop',
		render: function() {
			return $('#tpl_shop').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.shop();  
		}
	};
	
	//订单详情 
	var OrderDetails = {
		url: '/OrderDetails/*',
		className: 'OrderDetails',
		render: function() {
			return $('#tpl_OrderDetails').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.OrderDetails();  
		}
	};
	
	//发起群聊
	var GroupChat = {
		url: '/GroupChat/*',
		className: 'GroupChat',
		render: function() {
			return $('#tpl_GroupChat').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.GroupChat();  
		}
	};
	//发帖
	var Post = {
		url: '/Post',
		className: 'Post',
		render: function() {
			return $('#tpl_Post').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Post();  
		}
	};
	//首页
	var tpl_home = {
//		url: '/home',
//		className: 'home',
//		render: function() {
//			return $('#tpl_home').html();
//		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.home();
		}
	};
		//地图
	var gpsmap = {
		url: '/gpsmap/*/*',
		className: 'gpsmap',
		render: function() {
			return $('#tpl_gpsmap').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.gpsmap();
		}
	};
	
	//个人中心
	var Personal = {
		url: '/Personal',
		className: 'Personal1',
		render: function() {
			return $('#tpl_Personal').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Personal();
			pageFun.user();
		}
	};
	
	//购物车
	var Shopping_Cart = {
		url: '/Shopping_Cart',
		className: 'Shopping_Cart',
		render: function() {
			return $('#tpl_Shopping_Cart').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			//alert(1);
			pageFun.Shopping_Cart();  
		
		}
	};
	//关于我们
	var About_us = {
		url: '/About_us',
		className: 'About_us',
		render: function() {
			return $('#tpl_About_us').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm();
			pageFun.About_us();
		}
	};
	//软件介绍
	var About_us2 = {
		url: '/About_us2',
		className: 'About_us2',
		render: function() {
			return $('#tpl_About_us2').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm();
			pageFun.About_us2();
		}
	};
	//
	var Confirm_Order = {
		url: '/Confirm_Order/*',
		className: 'Confirm_Order',
		render: function() {
			return $('#tpl_Confirm_Order').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Confirm_Order();  
		}
	};
	//群聊信息
	var Chat = {
		url: '/Chat/*',
		className: 'Chat',
		render: function() {
			return $('#tpl_Chat').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Chat();
		}
	};
	//联系人
	var Contacts = {
		url: '/Contacts',
		className: 'Contacts',
		render: function() {
			return $('#tpl_Contacts').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Contacts();
		}
	};
	//发送消息 Miao 2016-12-09
	var FS_XX = {
		url: '/FS_XX/*',
		className: 'FS_XX',
		render: function() {
			return $('#tpl_FS_XX').html();
		},
		bind: function() {
			bindFunction();
			pageFun.FS_XX();
		}
	};
	//群聊天 Miao 2016-12-14
	var Group_chat = {
		url: '/Group_chat/*/*',
		className: 'Group_chat',
		render: function() {
			return $('#tpl_Group_chat').html();
		},
		bind: function() {
			bindFunction();
			pageFun.Group_text();
		}
	};
	//评论 
	var Comment = {
		url: '/Comment/*',
		className: 'Comment',
		render: function() {
			return $('#tpl_Comment').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Comment();  
		}
	};
	
	// 商品评论 
	var Comment2 = {
		url: '/Comment2/*',
		className: 'Comment2',
		render: function() {
			return $('#tpl_Comment2').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Comment2();  
		}
	};
	
	//群管理
	var qun = {
		url: '/qun/*',
		className: 'qun',
		render: function() {
			return $('#tpl_qun').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.qun();  
		}
	};
	//群组
	var Group = {
		url: '/Group',
		className: 'Group',
		render: function() {
			return $('#tpl_Group').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Group();  
		}
	};
	//商城
	var Shopping_Mall = {
		url: '/Shopping_Mall',
		className: 'Shopping_Mall',
		render: function() {
			return $('#tpl_Shopping_Mall').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Shopping_Mall();  
		}
	};
	//商品详情
	var Product_Details = {
		url: '/Product_Details/*',
		className: 'Product_Details',
		render: function() {
			return $('#tpl_Product_Details').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Product_Details();  
		}
	};
	//设置
	var Set_Up = {
		url: '/Set_Up',
		className: 'Set_Up',
		render: function() {
			return $('#tpl_Set_Up').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Set_Up();  
		}
	};
	//社区
	var Community = {
		url: '/Community',
		className: 'Community1',
		render: function() {
			return $('#tpl_Community').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Community();  
		}
	};
	//帖子
	var T_Z1 = {
		url: '/T_Z1/*',
		className: 'T_Z1',
		render: function() {
			return $('#tpl_T_Z1').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.T_Z1();  
		}
	};
	//申请加盟
	var A_T_J = {
		url: '/A_T_J',
		className: 'A_T_J1',
		render: function() {
			return $('#tpl_A_T_J').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.A_T_J();  
		}
	};
	//申请入驻
	var Settled = {
		url: '/Settled',
		className: 'Settled1',
		render: function() {
			return $('#tpl_Settled').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Settled();  
		}
	};
	//Miao 2016-12-08 店铺维护
	var Maintain = {
		url: '/Maintain',
		className: 'Maintain',
		render: function() {
			return $('#tpl_Maintain').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Maintain();  
		}
	};
	//添加好友
	var CreateGroup = {
		url: '/CreateGroup',
		className: 'CreateGroup',
		render: function() {
			return $('#tpl_CreateGroup').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.CreateGroup();  
		}
	};
	//添加好友
	var ADD_Group = {
		url: '/ADD_Group',
		className: 'ADD_Group',
		render: function() {
			return $('#tpl_ADD_Group').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.ADD_Group();
		}
	};
	//添加好友
	var ADD = {
		url: '/ADD',
		className: 'ADD',
		render: function() {
			return $('#tpl_ADD').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.ADD();  
		}
	};
	//我的
	var My = {
		url: '/My',
		className: 'My',
		render: function() {
			return $('#tpl_My').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.My();
		}
	};
	//我的订单
	var My_Order = {
		url: '/My_Order',
		className: 'My_Order',
		render: function() {
			return $('#tpl_My_Order').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.My_Order();  
		}
	};
	//我的棋牌室
	var My_Chess1 = {
		url: '/My_Chess1/*',
		className: 'My_Chess1',
		render: function() {
			return $('#tpl_My_Chess1').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.My_Chess1();  
		}
	};
	//我的消息
	var My_news = {
		url: '/My_news',
		className:'My_news',
		render:function(){
			return $('#tpl_My_news').html();
		},
		bind:function(){
			bindFunction();
			pageFun.My_news();
		}
	}
	//修改昵称
	var modify1 = {
		url: '/modify1',
		className: 'modify1',
		render: function() {
			return $('#tpl_modify1').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Personal();  
		}
	};
	//麻将资讯
	var information = {
		url: '/information/*/*',
		className: 'information',
		render: function() {
			return $('#tpl_information').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.information();  
		}
	};
	//麻将资讯内容
	var information_content = {
		url: '/information_content/*/*/*',
		className: 'information_content1',
		render: function() {
			return $('#tpl_information_content').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.information_content1();  
		}
	};
	//棋牌室列表
	var brand = {
		url: '/brand',
		className: 'brand',
		render: function() {
			return $('#tpl_brand').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.brand();  
		}
	};
	//预约订单
	var Order1 = {
		url: '/Order1',
		className: 'Order1',
		render: function() {
			return $('#tpl_Order1').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Order1();  
		}
	};
	//预约订单2
	var Order2 = {
		url: '/Order2',
		className: 'Order2',
		render: function() {
			return $('#tpl_Order2').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Order2();  
		}
	};
	
	//我的评价
	var My_evaluate = {
		url: '/My_evaluate/*',
		className: 'My_evaluate',
		render: function() {
			return $('#tpl_My_evaluate').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.My_evaluate();  
		}
	};
	//咨询客服
	var Z_X_KF = {
		url: '/Z_X_KF',
		className: 'Z_X_KF',
		render: function() {
			return $('#tpl_Z_X_KF').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Z_X_KF();  
		}
	};
	//引导页 Miao 11/28
	var YD = {
		url: '/YD',
		className: 'YD',
		render: function() {
			return $('#tpl_YD').html();
		},
		bind: function() {
			//var parm = _GetParm(); 
			pageFun.YD();
		}
	};
	//修改密码 Miao 11/29
	var modify2 = {
		url: '/modify2',
		className: 'modify2',
		render: function() {
			return $('#tpl_modify2').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.Set_Up();
		}
	};
	//待评价
	var YuYue = {
		url: '/YuYue',
		className: 'YuYue',
		render: function() {
			return $('#tpl_YuYue').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.YuYue();
		}
	};
	//待付款
	var FuKuan = {
		url: '/FuKuan',
		className: 'FuKuan',
		render: function() {
			return $('#tpl_FuKuan').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.FuKuan();
		}
	};
	//待发货
	var FaHuo = {
		url: '/FaHuo',
		className: 'FaHuo',
		render: function() {
			return $('#tpl_FaHuo').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.FaHuo();
		}
	};
	//待收货
	var ShouHuo = {
		url: '/ShouHuo',
		className: 'ShouHuo',
		render: function() {
			return $('#tpl_ShouHuo').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.ShouHuo();
		}
	};
 

	//
	var user = {
		url: '/user',
		className: 'user',
		render: function() {
			return $('#tpl_user').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.user();
		}
	};
	//列表
	var list111 = {
		url: '/list111',
		className: 'list111',
		render: function() {
			return $('#tpl_list111').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.list111();  
		}
	};
	//列表
	var webview = {
		url: '/webview/*',
		className: 'webview',
		render: function() {
			return $('#tpl_webview').html();
		},
		bind: function() {
			bindFunction();
			//var parm = _GetParm(); 
			pageFun.webview();  
		}
	};
	/*
	router.push(Address)
		.push(QQ)
		.push(WeChat)
		.push(Repair)
		.push(shop)
		.push(OrderDetails)
		.push(GroupChat)
		.push(Post)
		.push(home)
		.push(Personal)
		.push(Shopping_Cart)
		.push(About_us)
		.push(About_us2)
		.push(Confirm_Order)
		.push(Chat)
		.push(Contacts)
		.push(Comment)
		.push(Comment2)
		.push(qun)
		.push(Group)
		.push(Shopping_Mall)
		.push(Product_Details)
		.push(Set_Up)
		.push(Community)
		.push(T_Z1)
		.push(A_T_J)
		.push(Settled)
		.push(ADD)
		.push(ADD_Group)
		.push(My)
		.push(My_Order)
		.push(My_Chess1)
		.push(My_news)
		.push(modify1)
		.push(information)
		.push(information_content)
		.push(Order1)
		.push(Order2)
		.push(My_evaluate)
		.push(Z_X_KF)
		.push(modify2)
		.push(EditAddress)
		.push(brand)
		.push(YuYue)
		.push(Maintain)
		.push(ShouHuo)
		.push(FaHuo)
		.push(FuKuan)
		.push(FS_XX)
		.push(CreateGroup)
		.push(Group_chat)
		.push(webview)
		.push(gpsmap)
//		.push()
//		.push()
//		.push()
//		.push(Upload_Avatar)
		.push(addressadd)
		.push(list111)
		.setDefault('/home') 
		.init();
		*/
	clearBug();

});



$(function() { 
	mui.plusReady(function() {
		var gt = plus.push.getClientInfo();
		fun.setCache("cid",gt.clientid);
		console.log(gt.clientid);
		plus.push.addEventListener( "click",function (msg) {
		// 分析msg.payload处理业务逻辑 
		alert( "You clicked: " + msg.content ); 
		}, false ); 
	});
	
	document.documentElement.style.webkitUserSelect='none';
	document.documentElement.style.webkitTouchCallout = "none"; 
	$("#tpl_Addresslist,#tpl_EditAddress,#tpl_clause,#tpl_Land,#tpl_QQ,#tpl_WeChat,#tpl_Repair,#tpl_orderAddrlist,#tpl_Personal,#tpl_modify1,#tpl_modify2,#tpl_bkl,#tpl_Password1,#tpl_bangdin,#tpl_Z_C,#tpl_Upload_Avatar,#tpl_YD").hide();
		
	pageFun.GPS();

	InitWebIM();
	var android_ios = navigator.userAgent;
	var isAndroid = android_ios.indexOf('Android') > -1 || android_ios.indexOf('Adr') > -1; //android终端
	var isiOS = !!android_ios.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	if(isAndroid == true){
	}else if(isiOS == true){
		$("head").append("<link>");
		css = $("head").children(":last");
		css.attr({
		    rel: "stylesheet",
		    type: "text/css",
		    href: "css/iosstyle.css"
		});
	} 

	$(".sPage").css("height",$(window).innerHeight()+'px');
	if(!localStorage.C){
		$("#tpl_YD").show();
		
		pageFun.YD();
	}else{
		$(".YD").remove(); 
	}
	if(!localStorage.token){
		pageFun.reg();
		$("#tpl_Land").show();
		$(".footer0,.footer9").hide();
		$("#Land_height").css("height",$(window).innerHeight()+'px');
		$("#tpl_Land").css("height",$(window).innerHeight()+'px');
		pageFun.login();
	}else{
		$("#tpl_Land").hide();
		pageFun.hxLogin();
	}
	
});

/***
 * 初始化全局的事件绑定
 */
function bindFunction() {
	
	//进入隐藏
	$(".footer0,.footer9,.G_W_C_Eject,.rgba,.update,.bk,.C_O_E,.Age,.GX,.Gender,#My-Maintain,#SR_input,.android-ics,.tan3,#bq").hide();
	//进入页面跳转最上
	$('body').scrollTop(0);
	$(".footer9 a,.footer0 a").click(function(){
		$(".footer9 a,.footer0 a").css("color","#000");
		$(this).css("color","#ff5000");
	});
	$(".footer li a p").css("color","#000");
	$(".footer9 li a p").css("color","#000");
	$("header .icon-zuojiantou").unbind('click').click(function(){
//		history.go(-1);
		window.history.back();
	});
	//弹出隐藏
	$(".icon-guanbi,.rgba").click(function(){
		$(".Eject").hide();
	});
	
	//删除上传图片按钮
	$(".remove").click(function(){
		$(this).parent(".tu").remove();
	});

	//滑块效果
	$(".btn2").click(function(){
		if($(this).hasClass("left")){
			$(this).removeClass("left");
			$(this).addClass("right");
			$(this).parent(".SetUp-btn").css("background-color","#FF5000")
		}else{
			$(this).removeClass("right");
			$(this).addClass("left");
			$(this).parent(".SetUp-btn").css("background-color","#bbb")
		}
	});
	//个人心中js
	$(".li3").click(function(){
		$(".Gender,.rgba").show();
	});
	$(".Gender ul li input").click(function(){
		$("input").removeClass("input2");
		$(this).addClass("input2");
	});
	$(".qx").click(function(){
		$(this).parents(".G_fixed").hide();
		$(".rgba").hide();
	});
	$(".li4").click(function(){
		$(".GX,.rgba").show();
		pageFun.user();
		$('.GX textarea').text(Y.sig);
	});

	$(".age_x").click(function(){
		$(".Age,.rgba").hide();
	});


    //tab切换
    $(".S_M_M_header li").click(function(){
		$(".S_M_M_header li").removeClass("border-btm");
		$(this).addClass("border-btm");
	});
}

/***
 * 处理输入框被弹出键盘遮挡的问题
 */
function clearBug() {
	if(/Android/gi.test(navigator.userAgent)) {
		window.addEventListener('resize', function() {
			if(document.activeElement.tagName == 'INPUT' || document.activeElement.tagName == 'TEXTAREA') {
				window.setTimeout(function() {
					document.activeElement.scrollIntoViewIfNeeded();
				}, 0);
			}
		})
	}
}